/*
 * Copyright (c) 2009-2013 EvTech Project
 * All rights reserved.
 * 
 * This file is part of Game packet.
 *
 * Game packet is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Game packet is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Game packet.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */package fi.honkalampisaatio.game.selection;

import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Rectangle2D;

import fi.honkalampisaatio.game.game.LanguageResource;
import fi.honkalampisaatio.game.ui.BasePanel;

public class LoadPanel extends BasePanel implements MouseListener {
	private static final long serialVersionUID = -5416270349161549955L;
	private String loadingText, extText;

	public LoadPanel() {
		refreshLanguage();
		extText = "";
		addMouseListener(this);
		// Create a new thread
	    Thread gameThread =  new Thread() {
	    	// Override run() to provide the running behavior of this thread.
	        @Override
	        public void run() {
	        	animationThread();
	        }};

		// Start the thread. start() calls run(), which in turn calls gameLoop().
		gameThread.start();
	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		if(loadingText!=null && extText!=null)
		{
			String text = loadingText+"..";
			Rectangle2D rc = null;
			double maxHeight = this.getHeight()/3;
			double maxWidth = this.getWidth()/2;
			float fontSize = 144;
	      	g.setFont(getFont().deriveFont(fontSize));
	      	
	      	rc = g.getFontMetrics().getStringBounds(text, g);
	      	if(rc.getHeight()>maxHeight) {
	      		fontSize = (int)(fontSize*maxHeight/rc.getHeight());
	          	g.setFont(getFont().deriveFont(fontSize));
	      	}
	      	
	      	rc = g.getFontMetrics().getStringBounds(text, g);
	      	if(rc.getWidth()>maxWidth) {
	      		fontSize = (int)(fontSize*maxWidth/rc.getWidth());
	          	g.setFont(getFont().deriveFont(fontSize));
	      	}

	      	rc = g.getFontMetrics().getStringBounds(text, g);
	      	g.drawString(loadingText+extText, (int)((this.getWidth()-rc.getWidth())/2), 
	      			(int)((this.getHeight()+rc.getHeight())/2-g.getFontMetrics().getDescent()));
		}
	}

	public void refreshLanguage()
	{
		LanguageResource dictionary = LanguageResource.getInstance();
		if(dictionary!=null)
			loadingText = dictionary.get("LOADING_TEXT");
		if(loadingText==null || loadingText.isEmpty())
			loadingText = "Ladataan";
	}
	
	public void animationThread()
	{
		while(true)
		{
			extText = "";
			this.repaint();
			for(int i=0;i<3;i++)
			{
				try {
					Thread.sleep(500);
					extText += ".";
					this.repaint();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
	}

}
